package controllers

import (
	"api_server/internal/services"
	"bytes"
	"encoding/json"
	"fmt"
	"golang.org/x/sync/errgroup"
	"jihe.common/protos/calculate_server"
	"strings"
	"time"

	"github.com/gin-gonic/gin"
	common "jihe.common"
	"jihe.common/oss"
	"jihe.common/protos/base_server"
	"jihe.common/protos/base_server/proto"
	calculate_server_proto "jihe.common/protos/calculate_server/proto"
	"jihe.common/protos/user_server"
	user_server_proto "jihe.common/protos/user_server/proto"
	"jihe.common/resp"
	"jihe.common/tool"
)

type BaseController struct {
	Redis common.Redis
}

func (s *BaseController) GetFashionTps(c *gin.Context) {
	ops := services.GetOptions("fashion_types")
	ret, err := base_server.BaseGetFashionTypes(c, ops)
	if err != nil {
		resp.RespInternalErr(c)
		return
	}
	resp.RespOk(c, ret.BaseFashionTypeList)
}

func (s *BaseController) GetFashionList(c *gin.Context) {
	req := &proto.BaseGetFashionListReq{}
	err := c.BindJSON(&req)
	if err != nil {
		resp.RespParamErr(c)
		return
	}
	if req.Page <= 0 {
		req.Page = 1
	}
	if req.PageSize <= 0 || req.PageSize > 100 {
		resp.RespParamErr(c)
		return
	}
	if req.Search == nil {
		resp.RespParamErr(c)
		return
	}
	req.Search.Status = 2 //只查上架的
	ret, err := base_server.BaseGetFashionList(c, req)
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c, services.FullFashionList(ret))
}

func (s *BaseController) GetRecommendedFashions(c *gin.Context) {
	ret, err := base_server.BaseGetRecommendedFashions(c, &proto.BaseNull{})
	if err != nil {
		fmt.Println(err)
		resp.RespInternalErr(c)
		return
	}
	resp.RespOk(c, services.FullFashionList(ret))
}

func (s *BaseController) GetFabricList(c *gin.Context) {
	ret, err := base_server.BaseGetListFabric(c, &proto.BaseGetListFabricReq{})
	if err != nil {
		resp.RespGeneralErr(c)
		return
	}
	resp.RespOk(c, ret)
}

func (s *BaseController) GetFabricDetail(c *gin.Context) {
	id := c.Query("id")
	if id == "" {
		resp.RespParamErr(c)
		return
	}
	ret, err := base_server.BaseGetDetailFabric(c, &proto.BaseFabric{
		Id: id,
	})
	if err != nil {
		resp.RespGeneralErr(c)
		return
	}
	resp.RespOk(c, ret)
}

func (s *BaseController) GetPatternDetail(c *gin.Context) {
	id := c.Query("id")
	if id == "" {
		resp.RespParamErr(c)
		return
	}
	ret, err := base_server.BaseGetPatternDetail(c, &proto.BasePattern{
		Id: id,
	})
	if err != nil {
		resp.RespGeneralErr(c)
		return
	}
	resp.RespOk(c, ret)
}

func (s *BaseController) GetFashionDetail(c *gin.Context) {
	id := c.Query("id")
	if id == "" {
		resp.RespParamErr(c)
		return
	}
	ret, err := base_server.BaseGetFashionDetail(c, &proto.BaseGetFashionDetailReq{
		Id: id,
	})
	if err != nil {
		resp.RespInternalErr(c)
		return
	}
	resp.RespOk(c, ret.Fashion)
}

func (s *BaseController) GetFashionCutpartList(c *gin.Context) {
	id := c.Query("id")
	if id == "" {
		resp.RespParamErr(c)
		return
	}
	ret, err := base_server.GetCutpartList(c, &proto.GetCutpartListReq{
		FashionId: id,
	})
	if err != nil {
		resp.RespInternalErr(c)
		return
	}
	resp.RespOk(c, ret.BaseCutpartList)
}

func (s *BaseController) GetPatternList(c *gin.Context) {
	req := proto.BaseGetPatternListReq{}
	err := c.BindJSON(&req)
	if err != nil {
		resp.RespParamErr(c)
		return
	}
	if req.Page <= 0 {
		req.Page = 1
	}
	if req.PageSize <= 0 {
		req.PageSize = 20
	}
	if req.PageSize > 100 {
		resp.RespParamErr(c)
		return
	}
	ret, err := base_server.BaseGetPatternList(c, &req)
	if err != nil {
		common.Loger.Error("GetPatternList err:%v", err)
		resp.RespInternalErr(c)
		return
	}
	resp.RespOk(c, ret)
}

func (s *BaseController) MakeDesignQrcode(c *gin.Context) {
	uuid := tool.GetSnowflakeId()
	png, err := tool.MakeQrcode(fmt.Sprintf("%d", uuid))
	if err != nil {
		resp.RespInternalErr(c)
		return
	}
	bucket, err := oss.GetClient().Bucket("jihe-file")
	if err != nil {
		resp.RespInternalErr(c)
		return
	}
	path := fmt.Sprintf("%s/%d.png", time.Now().Format("2006-01-02"), uuid)
	err = bucket.PutObject(path, bytes.NewReader(png))
	if err != nil {
		resp.RespInternalErr(c)
		return
	}
	url := fmt.Sprintf("https://files.dizanz.com/%s", path)
	ret := make(map[string]string)
	ret["url"] = url
	ret["uuid"] = fmt.Sprintf("%d", uuid)
	err = s.Redis.Get().Set(s.Redis.GetBaseDesignScanKey(fmt.Sprintf("%d", uuid)), `{"status":"waiting"}`, time.Hour*2).Err()
	if err != nil {
		resp.RespInternalErr(c)
		return
	}
	resp.RespOk(c, ret)
}

func (s *BaseController) GetDesignScanResult(c *gin.Context) {
	uuid := c.Query("uuid")
	if uuid == "" {
		resp.RespParamErr(c)
		return
	}
	retMap := make(map[string]string)
	result, _ := s.Redis.Get().Get(s.Redis.GetBaseDesignScanKey(uuid)).Result()
	if result == "" {
		retMap["status"] = "invalid"
		resp.RespOk(c, retMap)
		return
	}
	err := json.Unmarshal([]byte(result), &retMap)
	if err != nil {
		retMap["status"] = "invalid"
		resp.RespOk(c, retMap)
		return
	}
	if retMap["status"] == "ok" {
		err = s.Redis.Get().Del(s.Redis.GetBaseDesignScanKey(uuid)).Err()
		if err != nil {
			resp.RespInternalErr(c)
			return
		}
		resp.RespOk(c, retMap)
		return
	}
	resp.RespOk(c, retMap)
}

func (s *BaseController) PreCalculate(c *gin.Context) {
	needId := c.Query("need_id")
	if needId == "" {
		resp.RespParamErr(c)
		return
	}
	err := services.PreCalculate(c, needId, "admin")
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c)
}

func (s *BaseController) SetDesignScanResult(c *gin.Context) {
	userInfo, err := user_server.UserGetByUid(c, &user_server_proto.UserDUser{
		Id: getUid(c),
	})
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	if userInfo.Status != 1 {
		resp.RespGeneralErr(c, "账号已被禁用")
		return
	}
	if userInfo.Phone == "" {
		resp.RespGeneralErr(c, "请先进行设计师认证")
		return
	}
	uuid := c.Query("uuid")
	req := make(map[string]string)
	err = c.BindJSON(&req)
	if err != nil {
		resp.RespParamErr(c)
		return
	}
	result, _ := s.Redis.Get().Get(s.Redis.GetBaseDesignScanKey(uuid)).Result()
	if result == "" {
		resp.RespGeneralErr(c, "二维码已失效")
		return
	}
	retMap := make(map[string]string)
	err = json.Unmarshal([]byte(result), &retMap)
	if err != nil {
		resp.RespGeneralErr(c, "二维码已失效")
		return
	}
	if retMap["status"] != "waiting" {
		resp.RespGeneralErr(c, "二维码已失效")
		return
	}
	req["status"] = "ok"
	reqB, err := json.Marshal(req)
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}

	err = services.PreCalculate(c, req["need_id"], "client")
	if err != nil {
		if strings.Contains(err.Error(), "errorno=1") {
			resp.RespGeneralErr(c, "款式已下架")
			return
		}
		resp.RespInternalErr(c, "debug"+err.Error())
		return
	}
	//处理完毕，正常扫码
	err = s.Redis.Get().Set(s.Redis.GetBaseDesignScanKey(uuid), reqB, time.Hour*2).Err()
	if err != nil {
		resp.RespInternalErr(c)
		return
	}
	resp.RespOk(c)
}

func (s *BaseController) CreateDiyPattern(c *gin.Context) {
	req := proto.BaseDiyPattern{}
	err := c.BindJSON(&req)
	if err != nil {
		resp.RespParamErr(c)
		return
	}
	if req.Name == "" || req.Url == "" {
		resp.RespParamErr(c)
		return
	}
	req.Uid = getUid(c)
	_, err = base_server.BaseDiyPatternCreate(c, &req)
	if err != nil {
		resp.RespInternalErr(c)
		return
	}
	resp.RespOk(c)
}

func (s *BaseController) GetDiyPattern(c *gin.Context) {
	ret, err := base_server.BaseDiyPatternList(c, &proto.BaseDiyPattern{Uid: getUid(c)})
	if err != nil {
		resp.RespInternalErr(c)
		return
	}
	resp.RespOk(c, ret)
}

func (s *BaseController) CreateWorksCollection(c *gin.Context) {
	req := proto.BaseWorksCollectionWorks{}
	err := c.BindJSON(&req)
	if err != nil {
		resp.RespParamErr(c)
		return
	}
	req.Collection.Uid = getUid(c)
	if req.Collection.Cover == "" || req.Collection.Name == "" || len(req.List) == 0 {
		resp.RespParamErr(c)
		return
	}
	_, err = base_server.BaseCreateWorksCollection(c, &req)
	if err != nil {
		resp.RespParamErr(c)
		return
	}
	resp.RespOk(c)
}

func (s *BaseController) UpdateWorksCollection(c *gin.Context) {
	req := proto.BaseWorksCollectionWorks{}
	err := c.BindJSON(&req)
	if err != nil {
		resp.RespParamErr(c)
		return
	}
	req.Collection.Uid = getUid(c)
	if req.Collection.Cover == "" || req.Collection.Name == "" || len(req.List) == 0 || req.Collection.Id == "" {
		resp.RespParamErr(c)
		return
	}
	_, err = base_server.BaseUpdateWorksCollection(c, &req)
	if err != nil {
		resp.RespParamErr(c)
		return
	}
	resp.RespOk(c)
}

func (s *BaseController) GetWorksCollectionList(c *gin.Context) {
	uid := c.Query("uid")
	if uid == "" {
		uid = getUid(c)
	}
	ret, err := base_server.BaseGetWorksCollectionList(c, &proto.BaseWorksCollection{
		Uid: uid,
	})
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c, ret.List)
}

func (s *BaseController) GetWorksCollectionDetail(c *gin.Context) {
	id := c.Query("id")
	if id == "" {
		resp.RespParamErr(c)
		return
	}
	ret, err := base_server.BaseGetWorksCollectionDetail(c, &proto.BaseWorksCollection{
		Id: id,
	})
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c, ret)
}

func (s *BaseController) DeleteWorksCollection(c *gin.Context) {
	id := c.Query("id")
	if id == "" {
		resp.RespParamErr(c)
		return
	}
	_, err := base_server.BaseDeleteWorksCollection(c, &proto.BaseWorksCollection{
		Id: id,
	})
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c)
}

type CreateNeedReq struct {
	Need    proto.BaseCustomerRegistration `json:"need"`
	User    user_server_proto.UserCUser    `json:"user"`
	From    string                         `json:"from"`
	GroupId string                         `json:"group_id"`
	Cover   string                         `json:"cover"`
	Name    string                         `json:"name"`
}

func (s *BaseController) CreateNeed(c *gin.Context) {
	req := CreateNeedReq{}
	err := c.BindJSON(&req)
	if err != nil {
		resp.RespParamErr(c)
		return
	}
	if req.User.Phone == "" || req.User.Name == "" || req.User.Age <= 0 || req.User.Sex <= 0 {
		resp.RespParamErr(c)
		return
	}
	if req.From == "store" {
		if req.GroupId == "" {
			resp.RespParamErr(c)
			return
		}
	}
	bodyIsComplete := true //是否有量完体
	for _, v := range req.Need.BodyData {
		if v == "" {
			bodyIsComplete = false
			break
		}
	}
	if bodyIsComplete {
		req.Need.BodyData["complete"] = "1" //已量体
	} else {
		delete(req.Need.BodyData, "complete")
	}
	req.User.BodyData = req.Need.BodyData
	req.Need.Sex = req.User.Sex
	req.Need.DUid = getUid(c)
	cuser, err := user_server.UserCUserCreate(c, &req.User)
	if err != nil {
		if strings.Contains(err.Error(), "c_users_phone_uindex") {
			cuser, err = user_server.UserCUserUpdate(c, &req.User)
			if err != nil {
				resp.RespInternalErr(c, err.Error())
				return
			}
			req.User.Id = cuser.Id
			req.Need.CUid = cuser.Id
		} else {
			resp.RespInternalErr(c, err.Error())
			return
		}
	} else {
		req.User.Id = cuser.Id
		req.Need.CUid = cuser.Id
	}
	if req.User.Id == "" {
		resp.RespGeneralErr(c, "用户创建失败")
		return
	}
	var needDetail *proto.BaseCustomerRegistration
	var g errgroup.Group
	var storeFashionRet *proto.DStoreFashion
	g.Go(func() error {
		needDetail, err = base_server.BaseCreateCustomerRegistration(c, &req.Need)
		if err != nil {
			return err
		}
		if req.From == "store" {
			storeFashionRet, err = base_server.CreateStoreFashion(c, &proto.DStoreFashion{
				DUid:    getUid(c),
				GroupId: req.GroupId,
				Cover:   req.Cover,
				Name:    req.Name,
				NeedId:  needDetail.Id,
			})
		}
		if err != nil {
			return err
		}
		return nil
	})

	g.Go(func() error {
		_, err = user_server.UserCreateCDUser(c, &user_server_proto.UserCDUser{
			DUid: getUid(c),
			CUid: req.User.Id,
		})
		if err != nil {
			if strings.Contains(err.Error(), "c_d_users_d_uid_c_uid_uindex") {
				return nil
			}
			return err
		}
		return nil
	})
	err = g.Wait()
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	if storeFashionRet != nil {
		needDetail.FashionId = storeFashionRet.Id
	}

	resp.RespOk(c, needDetail)
}

func (s *BaseController) GetNeedList(c *gin.Context) {
	req := proto.BaseGetCustomerRegistrationListReq{}
	err := c.BindJSON(&req)
	if err != nil {
		resp.RespParamErr(c)
		return
	}
	if req.Page <= 0 {
		req.Page = 1
	}
	if req.PageSize <= 0 {
		req.PageSize = 20
	}
	if req.PageSize > 100 {
		resp.RespParamErr(c)
		return
	}
	retMap := make(map[string]interface{})
	req.DUid = getUid(c)
	list, err := base_server.BaseGetCustomerRegistrationList(c, &req)
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	if len(list.List) == 0 {
		resp.RespOk(c)
		return
	}
	cUsers := make(map[string]*user_server_proto.UserCUser)
	fashions := make(map[string]*proto.BaseFashion)
	for _, v := range list.List {
		cUsers[v.CUid] = nil
		if v.FashionId != "" {
			fashions[v.FashionId] = nil
		}
	}
	cUserIds := make([]string, 0)
	fashionIds := make([]string, 0)
	for cuid, _ := range cUsers {
		cUserIds = append(cUserIds, cuid)
	}
	for fid, _ := range fashions {
		fashionIds = append(fashionIds, fid)
	}
	if len(fashionIds) != 0 {
		retMap["fashions"], err = base_server.BaseGetFashionListByIds(c, &proto.BaseGetFashionListByIdsReq{
			Ids: fashionIds,
		})
		if err != nil {
			resp.RespInternalErr(c, err.Error())
			return
		}
	}
	retMap["list"] = list
	retMap["c_users"], err = user_server.UserGetCUsersByIds(c, &user_server_proto.UserGetCUsersByIdsReq{Uids: cUserIds})
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c, retMap)
}

func (s *BaseController) GetNeedDetail(c *gin.Context) {
	id := c.Query("id")
	if id == "" {
		resp.RespParamErr(c)
		return
	}
	retMap := make(map[string]interface{})
	detail, err := base_server.BaseGetCustomerRegistrationDetail(c, &proto.BaseCustomerRegistration{Id: id})
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	retMap["detail"] = detail
	retMap["c_user"], err = user_server.UserGetCUsersByIds(c, &user_server_proto.UserGetCUsersByIdsReq{
		Uids: []string{detail.CUid},
	})
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	if detail.FashionId != "" {
		retMap["fashion"], err = base_server.BaseGetFashionDetail(c, &proto.BaseGetFashionDetailReq{
			Id: detail.FashionId,
		})
		if err != nil {
			resp.RespInternalErr(c, err.Error())
			return
		}
	}
	resp.RespOk(c, retMap)
}

func (s *BaseController) UpdateNeed(c *gin.Context) {
	req := CreateNeedReq{}
	err := c.BindJSON(&req)
	if err != nil {
		resp.RespParamErr(c)
		return
	}
	if req.Need.Id == "" || req.User.Phone == "" || req.User.Name == "" || req.User.Age <= 0 || req.User.Sex <= 0 {
		resp.RespParamErr(c)
		return
	}
	bodyIsComplete := true //是否有量完体
	for _, v := range req.Need.BodyData {
		if v == "" {
			bodyIsComplete = false
			break
		}
	}
	if bodyIsComplete {
		req.Need.BodyData["complete"] = "1" //已量体
	} else {
		delete(req.Need.BodyData, "complete")
	}
	req.Need.Sex = req.User.Sex
	req.User.BodyData = req.Need.BodyData
	req.Need.DUid = getUid(c)

	needDetail, err := base_server.BaseGetCustomerRegistrationDetail(c, &proto.BaseCustomerRegistration{Id: req.Need.Id})
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	if needDetail.ObjUrl != "" { //已经设计过了
		//不允许重新选款
		if needDetail.FashionId != req.Need.FashionId {
			resp.RespGeneralErr(c, "不能重新选款")
			return
		}
	}
	cuser, err := user_server.UserCUserCreate(c, &req.User)
	if err != nil {
		if strings.Contains(err.Error(), "c_users_phone_uindex") {
			cuser, err = user_server.UserCUserUpdate(c, &req.User)
			if err != nil {
				resp.RespInternalErr(c, err.Error())
				return
			}
			req.User.Id = cuser.Id
			req.Need.CUid = cuser.Id
		} else {
			resp.RespInternalErr(c, err.Error())
			return
		}
	} else {
		req.User.Id = cuser.Id
		req.Need.CUid = cuser.Id
	}
	var g errgroup.Group
	g.Go(func() error {
		_, err = base_server.BaseUpdateCustomerRegistration(c, &req.Need)
		if err != nil {
			return err
		}
		return nil
	})
	g.Go(func() error {
		_, err = user_server.UserCreateCDUser(c, &user_server_proto.UserCDUser{
			DUid: getUid(c),
			CUid: req.User.Id,
		})
		if err != nil {
			if strings.Contains(err.Error(), "c_d_users_d_uid_c_uid_uindex") {
				return nil
			}
			return err
		}
		return nil
	})
	err = g.Wait()
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c)
}

func (s *BaseController) GetCustomerList(c *gin.Context) {
	cuserIds, err := user_server.UserGetCDUsersByDUid(c, &user_server_proto.UserCDUser{
		DUid: getUid(c),
	})
	if err != nil {
		resp.RespInternalErr(c)
		return
	}
	if len(cuserIds.Uids) == 0 {
		resp.RespOk(c)
		return
	}
	cusers, err := user_server.UserGetCUsersByIds(c, &user_server_proto.UserGetCUsersByIdsReq{
		Uids: cuserIds.Uids,
	})
	if err != nil {
		resp.RespInternalErr(c)
		return
	}
	resp.RespOk(c, cusers)
}

func (s *BaseController) UpdateNeedById(c *gin.Context) {
	req := proto.BaseCustomerRegistration{}
	err := c.BindJSON(&req)
	if err != nil {
		resp.RespParamErr(c)
		return
	}
	if req.Id == "" {
		resp.RespParamErr(c)
		return
	}
	if len(req.Photos) > 1 {
		needInfo, _ := base_server.BaseGetCustomerRegistrationDetail(c, &proto.BaseCustomerRegistration{Id: req.Id})
		if needInfo != nil {
			cuserInfo, err := user_server.UserGetCUsersByIds(c, &user_server_proto.UserGetCUsersByIdsReq{Uids: []string{needInfo.CUid}})
			if err != nil {
				resp.RespInternalErr(c, err.Error())
				return
			}
			if len(cuserInfo.List) == 0 {
				resp.RespInternalErr(c, "用户不存在")
				return
			}
			photos := make(map[string]string)
			photos["zhengmian"] = req.Photos[0]
			photos["cemian"] = req.Photos[1]
			//photosBytes, _ := json.Marshal(photos)
			_, err = user_server.UserCUserUpdate(c, &user_server_proto.UserCUser{
				Phone:  cuserInfo.List[0].Phone,
				Photos: photos,
			})
			if err != nil {
				resp.RespInternalErr(c, err.Error())
				return
			}
		}
	}

	_, err = base_server.BaseUpdateCustomerRegistration(c, &req)
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c)
}

func (s *BaseController) GetFashionFabricList(c *gin.Context) {
	fashionId := c.Query("fashion_id")
	if fashionId == "" {
		resp.RespParamErr(c)
		return
	}
	ret, err := base_server.BaseGetFashionFabrics(c, &proto.BaseGetFashionFabricsReq{
		FashionId: fashionId,
	})
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c, ret)
}

func (s *BaseController) GetCutPartsByNeedId(c *gin.Context) {
	needId := c.Query("need_id")
	if needId == "" {
		resp.RespParamErr(c)
		return
	}
	needCuts, err := base_server.BaseGetCutpartByNeedId(c, &proto.BaseNeedCutpart{NeedId: needId})
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c, needCuts.List)
}

func (s *BaseController) GetCutPartsCalculateStatusByNeedId(c *gin.Context) {
	needId := c.Query("need_id")
	if needId == "" {
		resp.RespParamErr(c)
		return
	}
	needCuts, err := calculate_server.CalculateGetCutpartQueueByNeedId(c, &calculate_server_proto.CalculateCutpartQueue{NeedId: needId})
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c, needCuts.List)
}

func (s *BaseController) UpdateNeedCutpart(c *gin.Context) {
	req := proto.BaseNeedCutpart{}
	err := c.BindJSON(&req)
	if err != nil {
		resp.RespParamErr(c)
		return
	}
	if req.CutpartId == "" {
		resp.RespParamErr(c)
		return
	}
	_, err = base_server.BaseUpdateNeedCutpartByCutpartId(c, &proto.BaseNeedCutpart{
		CutpartId: req.CutpartId,
	})
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c)
}

func (s *BaseController) GetStoreGroupList(c *gin.Context) {
	ret, err := base_server.GetStoregroupList(c, &proto.GetStoregroupListReq{
		DUid: getUid(c),
	})
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c, ret)
}

func (s *BaseController) GetStoreGroupFashionList(c *gin.Context) {
	groupId := c.Query("group_id")
	if groupId == "" {
		resp.RespParamErr(c)
		return
	}
	ret, err := base_server.GetStoreGroupFashion(c, &proto.GetStoreGroupFashionReq{
		GroupId: groupId,
	})
	if err != nil {
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c, ret)
}

func (s *BaseController) CreateStoreGroup(c *gin.Context) {
	req := proto.DStoreGroup{}
	err := c.BindJSON(&req)
	if err != nil {
		resp.RespParamErr(c, err.Error())
		return
	}
	req.DUid = getUid(c)
	if req.Cover == "" || req.Name == "" {
		resp.RespParamErr(c)
		return
	}
	ret, err := base_server.CreateStoregroup(c, &req)
	if err != nil {
		if strings.Contains(err.Error(), "d_store_groups_name_d_uid_uindex") {
			resp.RespGeneralErr(c, "已相同系列名")
			return
		}
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c, ret)
}

func (s *BaseController) CreateStore(c *gin.Context) {
	req := proto.DStore{}
	err := c.BindJSON(&req)
	if err != nil {
		resp.RespParamErr(c, err.Error())
		return
	}
	if req.Name == "" {
		resp.RespParamErr(c)
		return
	}
	req.DUid = getUid(c)
	ret, err := base_server.CreateStore(c, &req)
	if err != nil {
		if strings.Contains(err.Error(), "d_stores_d_uid_uindex") {
			resp.RespOk(c)
			return
		}
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c, ret)
}

func (s *BaseController) GetStoreDetail(c *gin.Context) {
	ret, err := base_server.GetStoreByDuid(c, &proto.GetStoreByDuidReq{
		DUid: getUid(c),
	})
	if err != nil {
		if strings.Contains(err.Error(), "rows") {
			resp.RespOk(c)
			return
		}
		resp.RespInternalErr(c, err.Error())
		return
	}
	resp.RespOk(c, ret)
}
